package com.oocl.euc.exceptionhandler;

import com.oocl.euc.framework.exception.CustomException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseEntity handler(Exception e) {
        StringBuilder stringBuilder = new StringBuilder();
        if (e instanceof CustomException) {
            stringBuilder.append(e.getMessage());
        } else {
            stringBuilder.append("system error");
        }
        logger.error("error", e);
        return new ResponseEntity(stringBuilder.toString(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
